
from Framework.Downloader.LinkDownloader import LinkDownloader
from Common.logRoutine import *
import traceback
import threading

class ThreadedLinkDownloader( threading.Thread ):

  def __init__(self, config,  **kwds):
    threading.Thread.__init__(self, **kwds)
    
    self.link_downloader = LinkDownloader( config )
    
    self.config = config
    self.log = getLogger()
    
    self.latest_links = []
    self.have_new_results = False
    
    
  def generate_latest_links( self ):
    """
    Spawn thread, get links, process etc.
    """
    
    self.log.info("ThreadedLinkDownloader::generate_latest_links start")
    
    self.latest_links = self.link_downloader.getAllUniqueEntries()
    self.have_new_results = True
    
    
  def get_latest_links( self ):
    """
    Should just be a property of this class!!!
    """
    
    self.log.info("ThreadedLinkDownloader::get_latest_links start")
    
    self.have_new_results = False
    return self.latest_links

  def get_percent_complete( self ):
    """
    Should just be a property of this class!!!
    """
    
    #self.log.info("ThreadedLinkDownloader::get_days_downloaded_so_far start")
    
    return self.link_downloader.percent_complete
  
  
    
  def run( self ):
    
    self.log.info("ThreadedLinkDownloader::run start")
    
    try:
    
      self.have_new_results = False
      self.generate_latest_links()
      
      # force an exit. hacky
      self.link_downloader.day_count=6
      
      self.log.debug("Leaving ThreadedLinkDownloader")
      
    except:
      self.log.error("ThreadedLinkDownloader::run ex " + traceback.format_exc() )
      
    